package ac.aiit.tw;

import org.apache.commons.configuration.Configuration;

import twitter4j.TwitterStream;
import twitter4j.TwitterStreamFactory;
import twitter4j.auth.AccessToken;

public class twFactory {
	private static final String _consumerKey = "PGOsA2vEaPJr9UUqXWvww";
	private static final String _consumerSecret = "QVSVRy2ynWspL5z6Zw620MbKJDbDsyg0icIpgy8";
	public static final String CFG_KEY_OAUTH = "oauth";
	public static final String CFG_KEY_OA_CONSUMER_KEY = "consumerKey";
	public static final String CFG_KEY_OA_CONSUMER_SECRET = "consumerSecret";
	public static final String CFG_KEY_OA_TOKEN = "accessToken";
	public static final String CFG_KEY_OA_TOKENSECRET = "accessTokenSecret";
	
	public static final TwitterStream getStreamInstance(Configuration config){
	    Configuration oacfg = config.subset("oauth");
	    if(null != oacfg
	    		&& oacfg.containsKey(CFG_KEY_OA_TOKEN) && oacfg.containsKey(CFG_KEY_OA_TOKENSECRET)){
		    TwitterStream twitterStream = new TwitterStreamFactory().getInstance();		    		

		    twitterStream.setOAuthConsumer(oacfg.getString(CFG_KEY_OA_CONSUMER_KEY,_consumerKey)
		    		,oacfg.getString(CFG_KEY_OA_CONSUMER_SECRET, _consumerSecret));
		    twitterStream.setOAuthAccessToken(new AccessToken(oacfg.getString(CFG_KEY_OA_TOKEN), oacfg.getString(CFG_KEY_OA_TOKENSECRET)));
		    return twitterStream;
		}
	    return null;
	}
}
